package com.gzsxy.esjy.service.netty.controller;


import com.gzsxy.esjy.mapping.annotation.Controller;
import com.gzsxy.esjy.mapping.annotation.Param;
import com.gzsxy.esjy.mapping.annotation.RequestBody;
import com.gzsxy.esjy.mapping.annotation.RequestMapping;
import com.gzsxy.esjy.mapping.annotation.enums.RequestMethod;
import com.gzsxy.esjy.response.DefaultResponse;
import com.gzsxy.esjy.service.netty.dto.ExpiredResourceDto;
import com.gzsxy.esjy.service.netty.dto.UploadDto;
import com.gzsxy.esjy.service.netty.entity.ResourcesIndex;
import com.gzsxy.esjy.service.netty.service.FileUploadService;
import io.netty.handler.codec.http.multipart.FileUpload;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * 文件服务控制层
 * @author zhangxiaolong
 * @date 2022/7/11 9:36
 */
@Controller
public class FileUploadController {

    @Autowired
    private FileUploadService fileUploadService;

    /**
     * 文件上传base64
     *
     * @param dto
     * @return
     * @throws Exception
     */
    @RequestMapping(method = RequestMethod.POST, value = "/upload.json")
    public DefaultResponse upLoadFile(@RequestBody UploadDto dto) throws Exception {
        ResourcesIndex resourcesIndex = fileUploadService.saveFile(dto);
        return DefaultResponse.ok(resourcesIndex);
    }

    /**
     * 文件上传，表单提交
     *
     * @param file
     * @return
     * @throws IOException
     */
    @RequestMapping(method = RequestMethod.POST, value = "/upload.form")
    public DefaultResponse upLoadFile(@Param("file") FileUpload file, @Param("expired") Integer expired) throws IOException {
        ResourcesIndex resourcesIndex = fileUploadService.saveFile(null, file, expired);
        return DefaultResponse.ok(resourcesIndex);
    }

    /**
     * 文件上传，表单提交（批量上传）
     *
     * @param file
     * @return
     * @throws IOException
     */
    @RequestMapping(method = RequestMethod.POST, value = "/batchUpload.form")
    public DefaultResponse upLoadFile(@Param("expired") Integer expired, FileUpload[] file) throws IOException {
        List<ResourcesIndex> list = new ArrayList<>() ;
        for(FileUpload filep:file){
            list.add(fileUploadService.saveFile(null, filep, expired)) ;
        }
        return DefaultResponse.ok(list) ;
    }

    /**
     * 更新文件过期时间
     * @param index 文件索引
     * @param expried 时间/天 0代表永不过期
     * @return
     * @throws IOException
     */
    @RequestMapping(method = RequestMethod.GET, value = "/expiredResources")
    public DefaultResponse updateExpiredTime(String index, int expried) throws IOException {
        fileUploadService.updateExpiredTime(index, expried);
        return DefaultResponse.ok();
    }

    /**
     * 批量更新文件过期时间
     * @param lists
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/expiredListResources")
    public DefaultResponse updateFileByList(@RequestBody List<ExpiredResourceDto> lists) {
        fileUploadService.updateFileByList(lists);
        return DefaultResponse.ok();
    }


    /**
     * 根据fileIndex查询文件详情
     * @param fileIndex
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/getDetailByIndex")
    public DefaultResponse getDetailByIndex(@Param("fileIndex") String fileIndex){
        ResourcesIndex detailByIndex = fileUploadService.getDetailByIndex(fileIndex);
        return DefaultResponse.ok(detailByIndex);
    }



    @RequestMapping(method = RequestMethod.GET, value = "/getFileListByPath")
    public DefaultResponse getFileListByPath(@Param("filePath") String filePath) {
        if (StringUtils.isBlank(filePath)){
            return new DefaultResponse(null, 1, 400, "参数错误");
        }
//        List<ResourcesIndex> index = new ArrayList<>() ;
//        ResourcesIndex i = new ResourcesIndex() ;
//        i.setFileName("河智苑");
//        index.add(i) ;
        List<ResourcesIndex> index = fileUploadService.getFileListByPath(filePath);
        return new DefaultResponse(index, 1, 200, "success");
    }
}
